package com.huyong.android.weatherforecast;

import android.app.AlertDialog;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.huyong.android.weatherforecast.bean.WeatherBean;
import com.squareup.picasso.Picasso;

import java.util.List;

//http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo
public class CityWeatherFragment extends BaseFragment implements ViewGroup.OnClickListener{

    TextView tempTv,cityTv,conditionTv,windTv,tempRangeTv,dataTv,clothIndexTv,carIndexTv,coldIndexTv,sportIndexTv,raysIndexTv;
    ImageView dayIv;
    LinearLayout futureLayout;
    String url1="http://api.map.baidu.com/telematics/v3/weather?location=";
    String url2="&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo";
    private List<?> index;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_city_weather,container,false);
       initView(view);
        Bundle bundle = getArguments();
        String city = bundle.getString("city");
        String url=url1+city+url2;
        loadData(url);

        return view;
    }

    @Override
    public void onSuccess(String result) {
        System.out.println("success");
        parseShowData(result);
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        System.out.println("error ");
    }

    private void parseShowData(String result) {
        System.out.println("hello");
        //gson解析数据
        WeatherBean weatherBean = new Gson().fromJson(result, WeatherBean.class);
        WeatherBean.ResultsBean resultsBean = weatherBean.getResults().get(0);
        //获取指数信息集合列表
        index = resultsBean.getIndex();
        //设置TextView
        dataTv.setText(weatherBean.getDate());
        cityTv.setText(resultsBean.getCurrentCity());
        //今天天气情况
        WeatherBean.ResultsBean.WeatherDataBean todayDataBean = resultsBean.getWeather_data().get(0);
        windTv.setText(todayDataBean.getWind());
        tempRangeTv.setText(todayDataBean.getTemperature());
        conditionTv.setText(todayDataBean.getWeather());

        String[] split = todayDataBean.getDate().split("：");
        String replace = split[1].replace(")", "");
        tempTv.setText(replace);

        //天气图片
        Picasso.with(getActivity()).load(todayDataBean.getDayPictureUrl()).into(dayIv);

        //获取未来三天天气情况
        List<WeatherBean.ResultsBean.WeatherDataBean> futureList = resultsBean.getWeather_data();
        futureList.remove(0);
        for (int i = 0; i < futureList.size(); i++) {
            View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.item_main_center, null);
            itemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            futureLayout.addView(itemView);
            TextView iDateTv = itemView.findViewById(R.id.item_center_tv_date);
            TextView iConTv = itemView.findViewById(R.id.item_center_tv_con);
            TextView iTemprangeTv = itemView.findViewById(R.id.item_center_tv_temp);
            ImageView iTv=itemView.findViewById(R.id.item_center_iv);
            //获取对应位置天气情况
            WeatherBean.ResultsBean.WeatherDataBean dataBean = futureList.get(i);
            iDateTv.setText(dataBean.getDate());
            iConTv.setText(dataBean.getWeather());
            iTemprangeTv.setText(dataBean.getTemperature());
            Picasso.with(getActivity()).load(dataBean.getDayPictureUrl()).into(iTv);
        }
    }

    private void initView(View view){
        //用于初始化控件操作
        tempTv=view.findViewById(R.id.frag_tv_currenttemp);
        cityTv=view.findViewById(R.id.frag_tv_city);
        conditionTv=view.findViewById(R.id.frag_tv_condition);
        windTv=view.findViewById(R.id.frag_tv_wind);
        tempRangeTv=view.findViewById(R.id.frag_tv_temprange);
        dataTv=view.findViewById(R.id.frag_tv_date);

        coldIndexTv=view.findViewById(R.id.frag_tv_ganmo);
        clothIndexTv=view.findViewById(R.id.frag_tv_dress);
        carIndexTv=view.findViewById(R.id.frag_tv_washcar);
        sportIndexTv=view.findViewById(R.id.frag_tv_sport);
        raysIndexTv=view.findViewById(R.id.frag_tv_light);

        dayIv=view.findViewById(R.id.frag_tv_today);
        futureLayout=view.findViewById(R.id.frag_center_layout);

        clothIndexTv.setOnClickListener(this);
        carIndexTv.setOnClickListener(this);
        coldIndexTv.setOnClickListener(this);
        sportIndexTv.setOnClickListener(this);
        raysIndexTv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        switch (v.getId()) {
            case R.id.frag_tv_dress:
                builder.setTitle("穿衣指数");
                builder.setMessage("api暂停提供该服务!");
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_tv_washcar:
                builder.setTitle("洗车指数");
                builder.setMessage("api暂停提供该服务!");
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_tv_ganmo:
                builder.setTitle("感冒指数");
                builder.setMessage("api暂停提供该服务!");
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_tv_sport:
                builder.setTitle("运动指数");
                builder.setMessage("api暂停提供该服务!");
                builder.setPositiveButton("确定",null);
                break;
            case R.id.frag_tv_light:
                builder.setTitle("紫外线指数");
                builder.setMessage("api暂停提供该服务!");
                builder.setPositiveButton("确定",null);
                break;
        }
        builder.create().show();
    }
}